package com.zy.函数式编程.练习

import scala.annotation.tailrec

object TestWhile {
  def main(args: Array[String]): Unit = {

    // 定义一个函数 实现while循环的功能

    @tailrec
    def myWhile(condition: => Boolean)(optional: () => Any): () => Any = {
      if (!condition) return optional
      optional()
      myWhile(condition)(optional)
    }

    var i = 10
    myWhile(i > 0)(() => {
      println(i)
      i = i - 1
    })

    println("=================================")

    // 课上讲的
    def myWhile1(condition: => Boolean): (=> Unit) => Unit = {
      def doLoop(optional: => Unit): Unit = {
        if (condition) {
          optional
          myWhile1(condition)(optional)
        }
      }

      doLoop
    }

    i = 10
    myWhile1(i > 0) {
      println(i)
      i = i - 1
    }

    println("=================================")

    // 柯里化实现
    def myWhile2(condition: => Boolean)(optional: => Unit): Unit = {
      if (condition) {
        optional
        myWhile1(condition)(optional)
      }
    }

    i = 10
    myWhile2(i > 0) {
      println(i)
      i = i - 1
    }
  }
}
